/*
 * main.cpp
 * Michael du Breuil
 *
 * main control loop, handles task flow and communications
 */

#include "common.h"
#include "cam_fetch.h"
#include "ftp_put.h"
#include "mav_process.h"
#include "net_local.h"

image_t images[MAX_QUEUED_IMAGES];
pthread_mutex_t image_mutex;
pthread_cond_t image_cond;
unsigned int image_head, image_tail;

int main(int argc, char **argv) {
  int running = TRUE;
  pthread_t camera;
  pthread_t ftp;

  //init

  image_head = image_tail = 0;
  pthread_mutex_init(&image_mutex, NULL);
  pthread_cond_init(&image_cond, NULL);

  cam_init();
  ftp_init();
  mav_init();
  net_init();


  //FIXME: this should be gone once radio is up
  cam_settings.flags &= ~FLAG_PAUSE;
  ftp_settings.flags &= ~FLAG_PAUSE;

  pthread_create(&camera, NULL, cam_run, NULL); 
  pthread_create(&ftp, NULL, ftp_run, NULL); 

  while(running) {
    mav_messagesParse();
   
    net_accept();  //process any new clients
    net_process(); //handle any input

    //sleep as needed, this shouldn't need to run continously
    sleep(1);
  }

  //shutdown
  cam_shutdown();
  ftp_shutdown();
  mav_shutdown();

  //don't quit till the threads are done, and shutdown
  pthread_exit(NULL);

  return 0;
}
